package com.hy.controller.setting;

import com.hy.common.base.RespResult;
import com.hy.common.excel.UserExcelExport;
import com.hy.common.log.annotation.SysOpLogAnnotation;
import com.hy.entity.dto.HyPropertySearchDTO;
import com.hy.entity.dto.UpdateUserDTO;
import com.hy.entity.dto.UserSearchDTO;

import com.hy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/user")
public class HyUserController {


    @Autowired
    private SysUserService userService;


    @Autowired
    private UserExcelExport entityExcelExport;


    @RequestMapping("/importExcelBigData")
    @SysOpLogAnnotation(menuName = "用户",menuBtn = "导入用户",reqParam = "'用户导入'")
    public RespResult importExcelBigData(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){


        return  userService.importExcelBigData(multipartFile,request);
    }

    @GetMapping("/showUserDetail")
    @SysOpLogAnnotation(menuName = "用户",menuBtn = "查看用户",reqParam = "'查询条件'")
    public RespResult showUserDetail( UserSearchDTO userSearchDTO){

        return  userService.showUserByDto(userSearchDTO);
    }

    // 导出小数据量数据到excel

    @RequestMapping("/exportExcelSmallData")
    @SysOpLogAnnotation(menuName = "用户",menuBtn = "导出用户",reqParam = "'按条件导出用户'")
    public void export(HttpServletResponse response, UserSearchDTO searchDTO){
        entityExcelExport.exportWithSmallData(response, "用户列表", searchDTO);
    }
    @PostMapping("/addUser")
    @SysOpLogAnnotation(menuName = "用户",menuBtn = "添加用户",reqParam = "'用户ID:'+#hyUser.userId")
    public RespResult addUser(@RequestBody @Validated UpdateUserDTO hyUser){

        return userService.addUser(hyUser);
    }



    @PostMapping("/update")
    @SysOpLogAnnotation(menuName = "用户",menuBtn = "修改用户",reqParam = "'用户ID:'+#hyUser.userId")
    public RespResult update(@RequestBody UpdateUserDTO hyUser){
        if (hyUser.getDel().equals(2)){
            return RespResult.error(500,"不存在的资源");
        }
        return userService.updateUser(hyUser);
    }

    @PostMapping("/del")
    @SysOpLogAnnotation(menuName = "用户",menuBtn = "删除用户",reqParam = "'用户ID:'+#ids")
    public RespResult delete(Integer[] ids){
        List<Integer> integers=new ArrayList<>();
        Collections.addAll(integers,ids);
        return userService.deleteUser(integers);

    }






}
